//表格初始化
$(document).ready(function () {
    $('#tbodydata1').DataTable({
        // fixedHeader: true
    });
});
let optionJson = [];

function refurbishIndex_call() {
    // $("#resultcoll  tr").html("");
    let start_time = $("input[name='start_time']").val();
    let end_time = $("input[name='end_time']").val();
    let keyword = $("input[name='calling_num']").val();
    if (start_time == 0) {
        start_time = "2017-05-01";
    }
    if (end_time == 0) {
        end_time = "2017-09-30"
    }
    if (keyword == 0) {
        keyword = "13010071406"
    }
    let request_parameters = {
        "phoneNum": keyword,
        "startTime": start_time,
        "endTime": end_time
    };
    $.ajax({
        type: "post",
        url: "http://127.0.0.1:9998/show/get_call_record_complete",
        data: JSON.stringify(request_parameters),
        contentType: "application/json;charset=utf-8",
        async: false,
        success: function (call_record_complete_info) {
            console.log(call_record_complete_info);
            optionJson = call_record_complete_info;
            $("#tbodydata1").dataTable().fnDestroy();//还原初始化
            $('#tbodydata1').DataTable({
                // "serverSide":true,
                //  "paging":true,
                // "lengthMenu":[15,25,35,100,25],
                // "pagingType":"scrolling",
                // serverSide:true,
                // Processing:true,
                // deferRender:true,
                "bStateSave": true,//翻页时保存状态
                // iDisplayLength:10,
                // iDisplayStart:0,
                "ordering": false,//禁用全局排序
                "sort": "position",
                data: optionJson.data,
                //设置列数据
                columns: [
                    {
                        "data": 'callingNum',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {
                        "data": 'calledNum',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {
                        "data": 'startTime',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {
                        "data": 'lastTime',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {
                        "data": 'city',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    }
                ],
                //设置排序
                "order": [[1, 'asc']],
                //设置语言
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项搜索结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "未搜索到数据",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
            });
        }
    });
}

function Call_Excelexport() {
    //   console.log(call_record_complete_info);
    //输出base64编码
    //要导出的json数据
    const jsonData = optionJson.data;
    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    let str = `主叫电话,被叫电话,开始通话时间,持续通话时间,所在城市\n`;
    //增加\t为了不让表格显示科学计数法或者其他格式
    for (let i in jsonData) {
        for (let item in jsonData[i]) {
            str += `${jsonData[i][item] + '\t'},`;
        }
        str += '\n';
    }
    //encodeURIComponent解决中文乱码
    let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
    //通过创建a标签实现
    let link = document.createElement("a");
    link.href = uri;
    //对下载的文件命名
    link.download = "用户通话记录表.xls";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
